package com.ctf.soft.ms.service.manage.impl;

import com.ctf.soft.ms.dao.dto.manage.Relation;
import com.ctf.soft.ms.dao.mapper.manage.GroupMapper;
import com.ctf.soft.ms.dao.mapper.manage.GroupRelationMapper;
import com.ctf.soft.ms.dao.model.manage.Group;
import com.ctf.soft.ms.dao.model.manage.GroupRelation;
import com.ctf.soft.ms.service.impl.BaseService;
import com.ctf.soft.ms.utils.ResourceUtil;
import com.ctf.soft.ms.utils.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctf.soft.ms.service.manage.IGroupService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 名称： GroupServiceImpl<br>
 * 描述： 群组业务处理实现类<br>
 * 最近修改时间：2015/7/31 13:20<br>
 * 修改说明：
 *
 * @author jiajl
 */
@Service
public class GroupServiceImpl extends BaseService<Group> implements IGroupService {

    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private GroupRelationMapper groupRelationMapper;

    public List<Group> loadPageList(Map<String, Object> map) {
        int page = Integer.valueOf(map.get("page").toString());
        int rows = Integer.valueOf(map.get("rows").toString());
        int beginRowNum = (page - 1) * rows;
        int endRowNum = page * rows;
        map.put("beginRowNum", beginRowNum);
        map.put("endRowNum", endRowNum);
        map.put("db", ResourceUtil.getString("DATA_ALIAS"));
        return groupMapper.queryForPageList(map);
    }

    public int getPageCount(Map<String, Object> map) {
        return groupMapper.queryForPageCount(map);
    }

    public int saveOrUpdate(Relation<Group> relation) {
        int c = -1;
        String uuid = "";
        if (ToolsUtil.isEmpty(relation.getT().getId())) {
            uuid = ToolsUtil.getRandomForUUID();
            relation.getT().setId(uuid);
            c = insertSelective(relation.getT());
        } else {
            uuid = relation.getT().getId().trim();
            c = updateByPrimaryKeySelective(relation.getT());
            if (c > 0) {
                GroupRelation params = new GroupRelation();
                params.setDefaultGroup(uuid);
                groupRelationMapper.delete(params);
            }
        }
        if (c > 0) {
            if (!ToolsUtil.isEmpty(relation.getRelationCode())) {
                c = insertBatch(relation.getRelationCode(),
                        relation.getRelationName(), uuid);
            }
        }
        return c;
    }

    public int insertBatch(String relationCode, String relationName,
                           String defaultGroup) {
        List<GroupRelation> list = new ArrayList<GroupRelation>();
        String[] arrsCode = relationCode.split(",");
        String[] arrsName = relationName.split(",");
        for (int i = 0; i < arrsCode.length; i++) {
            GroupRelation groupRelation = new GroupRelation();
            int len = arrsCode[i].lastIndexOf("]") + 1;
            groupRelation.setId(ToolsUtil.getRandomForUUID());
            groupRelation.setDefaultGroup(defaultGroup);
            groupRelation.setRelationMark(arrsCode[i].substring(0, len));
            groupRelation.setRelationCode(arrsCode[i].substring(len,
                    arrsCode[i].length()));
            groupRelation.setRelationName(arrsName[i]);
            list.add(groupRelation);
        }
        return groupRelationMapper.insertBatch(list);
    }

    public List<GroupRelation> loadListByGroup(String defaultGroup) {
        GroupRelation params = new GroupRelation();
        params.setDefaultGroup(defaultGroup);
        return groupRelationMapper.select(params);
    }
}
